package Communication;
import Movement.NERoom;
import NursE.*;
import lejos.nxt.LCD;
import lejos.nxt.Motor;
import lejos.nxt.SensorPort;
import lejos.nxt.Sound;


public class NEMain {

	/**
	 * ----------------------------------------------------------------
	 * 								Attributes
	 * ----------------------------------------------------------------
	 */
	private static NECommunication com;
	
	protected NENurse nurse;
	
	/**
	 * @param args
	 * @throws InterruptedException 
	 */
	
	public static void main(String[] args){
		
		
		NEMain main = new NEMain();

		com = new NECommunication(main);
		com.start();
		/**
		 * Diameter in inch of the two wheels
		 */
		double wheelDiameter = 2.2f;
		/**
		 * Two wheels of Nurse
		 */
		NEWheel leftWheel 	= NEWheel.newWheelWithMotorAndDiameter(Motor.A, wheelDiameter);
		NEWheel	rightWheel 	= NEWheel.newWheelWithMotorAndDiameter(Motor.B, wheelDiameter);
		
		/**
		 * Head of Nurse
		 */
		NEHead head = new NEHead(Motor.C, SensorPort.S1);
		/**
		 * Distance in inch between the axes of the two wheels
		 */
		double width	= 4.8f;
		/**
		 * Nurse !
		 */
		main.nurse 	= NENurse.newNurseWithLeftAndRightWheelsAndHeadAndWidth(main, leftWheel, rightWheel, head, width);
		
		
	}
	
	public void sendToServeur(char c){
		this.com.send(c);
	}
	
	public void incident() {
		com.kill();
		
	}
	
	
	/*
	 * p = debut programmation avec p = numero d'enregistrement
	 * f = fin enregistrement
	 * g = aller a gauche de 45� 
	 * d = aller a droite de 45�
	 * a = avancer
	 * r = reculer
	 * s = stop
	 */
	public void interprete(char c) {
		if (c >= 'A' && c <= 'J') {
			int destination = this.fromCapitalLetterToInt(c);
			this.nurse.goTo(destination);
			
		}else {
		
			switch(c){
				case 'p':
					this.nurse.beginSave();
					break;
					
				case 'g':
					this.nurse.turnLeft();
					break;
					
				case 'd':
					this.nurse.turnRight();
					break;
					
				case 'f':
					this.nurse.stopSave();
					break;
					
				case 'a':
					this.nurse.moveForward();
					break;
					
				case 'r':
					this.nurse.moveBackward();
					break;
					
				case 's':
					this.nurse.stop();
					break;
					
				case '.':
					this.shutdown();
					break;
					
			}
		
		}
		
	}

	private int fromCapitalLetterToInt(char c) {
		switch (c){
		case 'A':
			return 0;
		case 'B':
			return 1;
		case 'C':
			return 2;
		case 'D':
			return 3;
		case 'E':
			return 4;
		case 'F':
			return 5;
		case 'G':
			return 6;
		case 'H':
			return 7;
		case 'I':
			return 8;
		case 'J':
			return 9;
		default :
			return 20;
		}
	}

	private void shutdown() {
		com.kill();
		lejos.nxt.NXT.exit(1);
	}

}
